<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AD Guid and User Account Clean Up</title>
</head>

<!--- NOTES:  This is a maintenance page only.  It checks the GUIDs in AD for changes in the UserAccount.  If accounts aren't found, they are disabled in Rosetta.  If changes are found, they are changed in RosettaStone, WFCSuite.UserAccount and WFCSuite.tSelf_Access.
--->



	<cfobject type="dotnet" name="objCreation" class="ADTools.Creation" assembly ="C:\Program Files\Southcentral Foundation\ADTools\ADTools.dll">    



                   	


  
<body>
<!---cfdump var="#qryGetActiveUserIDs#"--->
<cfoutput>
<!---FIRST SCRUB GUIDs where they don't exist in Rosetta--->
	<cfquery name="qryGetActiveSCFUserIDs" datasource="RosettaStone">
		scf_GetAllActiveSCFAccounts 
	</cfquery>
	
	<cfquery name="qryGetRecordWMissingGUIDs" dbtype="query">
		Select RosettaID, UserAccount 
		From qryGetActiveSCFUserIDs
		Where AD_GUID is null and UserAccount is Not Null
	</cfquery>
	
	#qryGetRecordWMissingGUIDs.recordCount#<BR />
	
	<cfloop query="qryGetRecordWMissingGUIDs">
		<!---cfset strGUID = objCreation.GetGUID("", "#UserAccount#@anthc.org", "10.100.1.101")/---> 
		<cfset strGUID = objCreation.GetGUID("#UserAccount#", "", "")>  
		<cfset intRosettaID = RosettaID>
		<cfset strUserAccount = UserAccount>
		 
		#intRosettaID# #strUserAccount# '#strGUID#'<BR />
		
		<cfif strGUID NEQ "">
			<cfquery name="qryUpdateRosettaGUID" datasource="RosettaStone">
				scf_UpdateGUID '#strGUID#',#intRosettaID#				
			</cfquery>
			
		<cfelseif Left(userAccount,4) NEQ "ANMC">
			<cfquery name="qryDisableRosettaRecord" datasource="RosettaStone">
				scf_DisableRosettaUser #intRosettaID#
			</cfquery>			
		</cfif>
		
	</cfloop><br />



<!---SECOND SCRUB UserIDs in Rosetta for SCF accounts where they have recently been updated and are out of sync --->

	<cfset intDisabled = 0>
	<cfset intUpdated = 0>
	
	<cfmail from="ADGUIDCleanup@scf.cc" to="sjohnston@scf.cc" subject="AD GUID Cleanup Report" type="html">
	
		<cfloop query="qryGetActiveSCFUserIDs">
			<cfset intWFCPersonID = 0>
			<cfset intHRPersonIDNo = 0>
			
			<cfif AD_GUID NEQ ''>
				<cfset strADUserID = objCreation.GetUserIDFromGUID("#AD_GUID#")> 
				<cfif not isdefined("strADUserID")>
					<cfset strADUserID = "">
				<cfelse>
					<cfset strADUserID = replace(strADUserID,"'","''", "All")>					
				</cfif>
				<cfset strRosUserID = #UserAccount#>   
				<cfset intRosettaID = #RosettaID#>
				<cfif WFC_PersonID NEQ "">
					<cfset intWFCPersonID = #WFC_PersonID#>
				</cfif>
				<cfif HRProd_PersonIDNo NEQ "">
					<cfset intHRPersonIDNo = #HRProd_PersonIDNo#>
				</cfif>
												
				<cfif strADUserID EQ "" and Left(userAccount,4) NEQ "ANMC">
					<!--- Person's account no longer exists.  DISABLE --->
					<cfset intDisabled = intDisabled+1>
					#intRosettaID# Ros:#strRosUserID# DISABLE<Br />
					<cfquery name="qryDisableRosettaRecord" datasource="RosettaStone">
						scf_DisableRosettaUser #intRosettaID#
					</cfquery>
					
				<cfelseif ucase(replace(strADUserID,"''","'", "All")) NEQ ucase(strRosUserID) and Left(userAccount,4) NEQ "ANMC">
					<cfset intUpdated = intUpdated+1>	
					<!--- Person's User Account has changed. UPDATE --->			
					#intRosettaID# AD:#strADUserID# Ros:#strRosUserID# *********DIFFERENT************  UPDATE Rosetta<BR />
					<cfquery name="qryUpdateRosettaUserAccount" datasource="RosettaStone">
						scf_UpdateUserAccount '#strADUserID#', #intRosettaID#
					</cfquery>
					<cfif intWFCPersonID NEQ 0 and intHRPersonIDNo NEQ 0>
						  UPDATE WFCSuite<BR />
						<cfquery name="qryUpdateWFCUserFields" datasource="EPAR">
							scf_SCFVM18_UpdateUserAccountInfo '#strADUserID#', '#strADUserID#@SouthcentralFoundation.com', #intWFCPersonID#, #intHRPersonIDNo#
						</cfquery>
					<cfelse>
						NO WFCSuite UPDATE<BR />
					</cfif>
				</cfif>
			</cfif>
		</cfloop>
		<BR />
		Disabled:  #intDisabled#<BR />
		Updated:  #intUpdated#<BR />
		<br />
	</cfmail>
	
Disabled:  #intDisabled#<BR />
Updated:  #intUpdated#<BR />
<br />
	
</cfoutput>
</body>
</html>

